package vista;

import logica.*;
import modelo.*;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class XYChartExample {
	public static void main(String[] args) {
	// Create a simple XY chart
	XYSeries series = new XYSeries("Aprox");
	XYSeries exacta = new XYSeries("Exacta");
	String ecuacion = "-x+t";
	float x0 = 1;
	float t0 = 0;
	float tn = 100;
	int n = 20000;
	int metodo;
	String solucionExacta = "2*E^(-t)+t-1";
	
	metodo = IModelo.EULER;
	Resultado res=null; 
	try {
		res = new Modelo().calcular(ecuacion, x0, t0, tn, n, metodo, solucionExacta);
	} catch (Exception e1) {
		e1.printStackTrace();
	}
	
	
	Enumeration<Float> e = res.getSolucionAprox().keys();
	Float obj;
	while (e.hasMoreElements()) {
		obj = e.nextElement();
		System.out.println("clave "+ obj +": " + res.getSolucionAprox().get(obj));
		series.add(obj, res.getSolucionAprox().get(obj));
	}

	e = res.getSolucionExacta().keys();
	while (e.hasMoreElements()) {
		obj = e.nextElement();
		System.out.println("clave "+ obj +": " + res.getSolucionAprox().get(obj));
		exacta.add(obj, res.getSolucionExacta().get(obj));
	}

	
	// Add the series to your data set
	XYSeriesCollection dataset = new XYSeriesCollection();
	dataset.addSeries(series);
	dataset.addSeries(exacta);
	// Generate the graph
	JFreeChart chart = ChartFactory.createXYLineChart(
	"XY Chart", // Title
	"x-axis", // x-axis Label
	"y-axis", // y-axis Label
	dataset, // Dataset
	PlotOrientation.VERTICAL, // Plot Orientation
	true, // Show Legend
	true, // Use tooltips
	false // Configure chart to generate URLs?
	);
	
	try {
	ChartUtilities.saveChartAsJPEG(new File("C:\\chart.jpg"), chart, 500, 300);
	} catch (IOException ex) {
	System.err.println("Problem occurred creating chart.");
	}
	}
	}